package ex2a.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import ex2a.controller.Controller;
import ex2a.gui.ProductsPanel;
import ex2a.gui.SemaphoresPanel;
import ex2a.gui.ThreadsPanel;

public class Frame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private JFrame mainFrame, productsFrame;
	private GridBagLayout lay = null;
	private JPanel productsPanel = null, threadsPanel = null, semaphoresPanel = null;
	private JPanel mainPanel = null;
	private Dimension minimumSize;
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem pause, start;
	private JSlider speed;
	private Controller controller;
	
	public Frame( String title ) {
		super( title );
		
		GridBagConstraints gbc = new GridBagConstraints();
		lay = new GridBagLayout();
		mainFrame = this;
		controller = Controller.getInstance();
		
		this.addWindowStateListener( new WindowStateListener() {

			@Override
			public void windowStateChanged(WindowEvent arg0) {
				if( arg0.getOldState() == ICONIFIED && arg0.getNewState() != ICONIFIED ) {
					controller.maximize( true );
				} else if( arg0.getNewState() == ICONIFIED ) {
					controller.maximize( ( productsFrame.getState() != ICONIFIED && productsFrame.isVisible() ) );
				}
			}
		});
		
		productsFrame = new JFrame();
		mainPanel = new JPanel();
		productsPanel = new ProductsPanel();
		threadsPanel = new ThreadsPanel();
		semaphoresPanel = new SemaphoresPanel();
		
		mainPanel.setLayout( lay );
		
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1;
		gbc.weighty = 1;
		
		productsFrame.add( productsPanel );
		
		//gbc.gridy = 0;
		//mainPanel.add( productsPanel, gbc );
		
		gbc.gridy = 0;
		gbc.weighty = 0.5;
		mainPanel.add( threadsPanel, gbc );
		
		gbc.gridy = 1;
		gbc.weighty = 0.5;
		mainPanel.add( semaphoresPanel, gbc );
		
		getContentPane().add( mainPanel );
		initMenu();
		
		minimumSize = new Dimension( 300, 300 );
		
		productsFrame.setMinimumSize( minimumSize );
		productsFrame.setSize( minimumSize );
		productsFrame.setLocationRelativeTo( this );
		productsFrame.setVisible( true );
		productsFrame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
		productsFrame.addWindowStateListener( new WindowStateListener() {

			@Override
			public void windowStateChanged(WindowEvent arg0) {
				if( arg0.getOldState() == ICONIFIED && arg0.getNewState() != ICONIFIED ) {
					controller.maximize( true );
				} else if( arg0.getNewState() == ICONIFIED ) {
					controller.maximize( mainFrame.getState() != ICONIFIED );
				}
			}
			
		});
		
		setMinimumSize( minimumSize );
		setSize( minimumSize );
		setLocationRelativeTo( null );
		setVisible( true );
		
		setDefaultCloseOperation( EXIT_ON_CLOSE );
	}
	
	public void initMenu() {
		JMenuItem productsOption = new JMenuItem( "Products" );
		menuBar = new JMenuBar();
		menu = new JMenu( "Menu" );
		
		start = new JMenuItem( "Start" );
		pause = new JMenuItem( "Pause" );
		speed = new JSlider( 0, 1000 );
		speed.setValue( 500 );
		
		menuBar.add( menu );
		menuBar.add( productsOption );
		
		menu.add( start );
		menu.add( pause );
		menu.add( new JLabel("Thread Speed") );
		menu.add( speed );
		
		productsOption.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent a) {
				productsFrame.setVisible( true );
			}
			
		});
		
		menu.addMenuListener( new MenuListener() {

			@Override
			public void menuCanceled(MenuEvent e) {	}

			@Override
			public void menuDeselected(MenuEvent e) {
				Controller.getInstance().speed = ( 1000 - speed.getValue() );
			}

			@Override
			public void menuSelected(MenuEvent e) {}
			
		});
		
		setJMenuBar( menuBar );
		
		start.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed( ActionEvent e ) {
				Controller.getInstance().start( true );
				start.setEnabled( false );
				pause.setEnabled( true );
			}
		});
		
		pause.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed( ActionEvent e ) {
				Controller.getInstance().start( false );
				start.setEnabled( true );
				pause.setEnabled( false );
			}
		});
		pause.setEnabled( false );
	}
}
